package com.nstar.base.controls
{
	import flash.display.DisplayObjectContainer;
	import flash.events.Event;
	import flash.events.FocusEvent;
	import flash.events.KeyboardEvent;
	import flash.events.MouseEvent;
	import flash.ui.Keyboard;
	
	import mx.controls.TextInput;
	import mx.controls.ToolTip;
	import mx.controls.Tree;
	import mx.controls.listClasses.IListItemRenderer;
	import mx.events.ListEvent;
	import mx.managers.ToolTipManager;
	
	import com.nstar.base.control.ClearTextInput;
	
	/**
	 * 树形选择提示输入栏
	 * 
	 * @author 李嘉陵
	 * @date 2007.1
	 */
	public class TreeSelectInput extends ClearTextInput
	{
		private var tree:Tree = new Tree();
		private var isset:Boolean = false;
		private var topContainer:DisplayObjectContainer;
		
		//记录是不是第一次获得焦点，如果是第一次就弹出tree，否则不弹
		private var firstFocus:Boolean = true; 
		
		//item提示
		private var itemTip;
		
		
		
		/**
		 * 树的相关信息
		 **/
		[Bindable]
		private var _treeData:XMLList;
		private var _treeLabelField:String = "@label";
		private var _treeHeight:Number = 200;
		private var _selectLabelField:String;
		private var _dataField:String;
		
		//"提示"对应的树属性
		private var _toolTipField:String = "data";
		
		public function set treeData(value:XMLList): void {
			_treeData = value;
		}
		public function get treeData():XMLList {
			return _treeData;
		}
		
		public function set treeLabelField(value:String):void {
			_treeLabelField = value;
		}
		
		public function get treeLabelField():String {
			return _treeLabelField;
		}
		
		public function set selectLabelField(value:String):void {
			_selectLabelField = value;
		}
		
		public function get selectLabelField():String {
			return _selectLabelField;
		}
		
		/**
		 * 为了照顾使用习惯，将使用时使用"@"作为前缀，在输入后去掉
		 **/
		public function set toolTipField(value:String):void {
			_toolTipField = value.substr(1);
		}
		
		public function get toolTipField():String {
			return _toolTipField;
		}
		
		public function get filterData():XMLList {
			var filter:String = this.text;
			if(filter == "")
				return _treeData;
			
			//var exp1:RegExp = /^[改]/
			var exp1:RegExp = new RegExp("^["+filter+"]");
			
			//var exp2:RegExp = /[^改][改]+/;
			var exp2:RegExp = new RegExp("[^"+filter+"]["+filter+"]+");


			var tree:XML =<root/>;
			var label:String;
			for each ( var element:XML in _treeData ) {
				label = element.@label.toString();
				if(label.match(exp1))
					tree.appendChild(element);
			}
			
			for each ( var element:XML in _treeData ) {
				label = element.@label.toString();
				if(exp2.test(label))
					tree.appendChild(element);
			}

			return tree.child("node");
		}
		
		override public function get value():String {
			//检查输入的值是否在@label中，如果在，就返回@data
			for each ( var element:XML in _treeData ) {
				if(element.@label == this.text) {
					return element.@data;
				}
			}
			
			//检查输入的值是否在@data中，如果在，就返回@data
			for each ( var element:XML in _treeData ) {
				if(element.@data == this.text) {
					return element.@data;
				}
			}
			
			return "";
		}
		

		
		public function TreeSelectInput(){
			super();
			
			this.doubleClickEnabled = true;
			
			//单击显示树
			this.addEventListener(MouseEvent.CLICK , clickShowTree);
			//双击显示树
			this.addEventListener(MouseEvent.DOUBLE_CLICK , showTree);
			//当输入是按”向下“键的时候显示树
			this.addEventListener(KeyboardEvent.KEY_DOWN, keyDownShowTree);
			//处理失去焦点的情况
			//this.addEventListener(FocusEvent.FOCUS_OUT, inputFocusOut);
			
			
			
			tree.includeInLayout = false;
			tree.visible = false;
			

		}
		
		/**
		 * 显示树形选择
		 * */
		private function showTree(event:Event):void {
			//trace("firstFocus: "+firstFocus);
		
			if(!isset) {
					//trace(treeData);
					
					//trace(treeLabelField);
					tree.labelField = treeLabelField;
					
					
					topContainer = this.parent;
					var rootName:String = this.toString();
					rootName = rootName.slice(0,rootName.indexOf("."));
					//trace("rootName:"+rootName);
					while(topContainer!= null && rootName != topContainer.toString() && topContainer.parent != null) {
					 		topContainer = topContainer.parent;
					 		//trace(topContainer);
					}
					topContainer.addChild(tree);
					 
					
					tree.addEventListener(ListEvent.ITEM_CLICK, clickSelect);
					tree.addEventListener(FocusEvent.FOCUS_OUT, hide);
					tree.addEventListener(KeyboardEvent.KEY_DOWN, keySelect);
					tree.addEventListener(MouseEvent.MOUSE_OVER ,mouseOverTree);
					
					//显示or隐藏Item提示的监听函数
					tree.addEventListener(ListEvent.ITEM_ROLL_OVER,showItemTip);
					tree.addEventListener(ListEvent.ITEM_ROLL_OUT,hideItemTip);
					
					tree.height = _treeHeight;
					 
					isset = true;
			}
					
			tree.dataProvider = filterData;
			//tree.getFocus();
				 
			//topContainer.setFocus(tree);
			//tree.setFocus();
			tree.x = this.getBounds(topContainer).x;
			tree.y = this.getBounds(topContainer).y+24;
			tree.width = this.width;
				
			tree.visible = true;
			
			
		}
		
		/**
		 * 单击显示树
		 * */
		private function clickShowTree(event:Event):void {
			if(firstFocus) {
				showTree(event);
			}
		}
		
		/**
		 * 处理输入时按”向下“键显示树
		 * */
		private function keyDownShowTree(event:KeyboardEvent):void {
			//trace("keyDownShowTree "+event.keyCode);
			showTree(event);
			/* if(event.keyCode == Keyboard.DOWN) {
				if(tree != null && tree.visible == false)
					showTree(event);
				else {
					tree.dataProvider = filterData;
					tree.visible = false;
					tree.visible = true;
					tree.setFocus();
				}
					
			} */
		}
		
		/**
		 * 当input失去焦点时的处理要隐藏树
		 * */
		private function inputFocusOut(event:Event):void {
			hide(event);
		}
		
		/**
		 * 隐藏树
		 * */
		private function hide(event:Event):void {
			//trace("hide");
			tree.visible = false;
			//firstFocus = true;
		}
		
		/**
		 * 当鼠标经过Tree时将焦点设置到它的上面
		 * */
		private function mouseOverTree(event:Event):void {
			//trace("mouseOverTree");
			tree.setFocus();
		}
		
		/**
		 * 将选择的树节点，放入输入框
		 * */		
		private function select(event:Event):void {
			var selectedNode=Tree(event.target).selectedItem;
			
			if(selectLabelField == null)
				this.text = selectedNode.attribute(_treeLabelField.substr(1,_treeLabelField.length-1));
			else
				this.text = selectedNode.attribute(selectLabelField);
			
		}
		
		/**
		 * 鼠标点击选择事件处理方法
		 * */
		private function clickSelect(event:Event):void {
			//trace(clickSelect);
			tree.visible = false;
			select(event);
			firstFocus = false;
			this.setFocus();
		}
		
		/**
		 * 可以通过"回车"选择树的节点
		 * */
		private function keySelect(event:KeyboardEvent):void {
			//trace("keySelect："+event.keyCode)
			if(event.keyCode == Keyboard.ENTER) {
				tree.visible = false;
				select(event);
				firstFocus = false;
				this.setFocus();
			}
		}
		
		/**
		 * 显示每个节点的“提示”
		 **/
		private function showItemTip(event:ListEvent):void {
			var item:IListItemRenderer = event.itemRenderer;
			var tipString:String = item.data.attribute(_toolTipField);
			if(itemTip){
				ToolTipManager.destroyToolTip(itemTip);
			}
			if(tipString != "")
				itemTip = ToolTipManager.createToolTip(tipString,this.root.mouseX+10,this.root.mouseY+10);
		}
		
		/**
		 * 隐藏每个节点的“提示”
		 **/
		private function hideItemTip(event:ListEvent):void {
			if(itemTip){
				itemTip.setVisible(false);
			}
		}
	}
}